package com.kylin.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kylin.eduservice.entity.EduSubject;
import com.kylin.eduservice.entity.excel.SubjectData;
import com.kylin.eduservice.entity.subject.OneSubject;
import com.kylin.eduservice.entity.subject.TwoSubject;
import com.kylin.eduservice.listener.SubjectExcelListener;
import com.kylin.eduservice.mapper.EduSubjectMapper;
import com.kylin.eduservice.service.EduSubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author kylin
 * @since 2020-07-15
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Autowired
    SubjectExcelListener subjectExcelListener;


    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            //获取文件输入流
            InputStream inputStream = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(inputStream, SubjectData.class, subjectExcelListener).sheet().doRead();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    //课程分类列表（树型）
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询所有一级分类 parentid=0
        QueryWrapper<EduSubject> oneWrapper = new QueryWrapper();
        oneWrapper.eq("parent_id", "0");
        //调用mapper
        List<EduSubject> oneSubjectList = baseMapper.selectList(oneWrapper);
        //调用service本身
        //List<EduSubject> list = this.list(oneWrapper);

        //查询所有二级分类 parentid!=0
        QueryWrapper<EduSubject> twoWrapper = new QueryWrapper<>();
        twoWrapper.ne("parent_id", "0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(twoWrapper);

        //创建list集合用于封装最终数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        //封装一级分类
        for (EduSubject subject : oneSubjectList) {
            OneSubject oneSubject = new OneSubject();
            //oneSubject.setId(subject.getId());
            //oneSubject.setTitle(subject.getTitle());
            //简化 将edu中的对应的值get出来在set到oneSubject中
            BeanUtils.copyProperties(subject, oneSubject);

            //在一级分类循环遍历查询所有的二级分类
            //创建list集合将每一个二级分类封装到一级分类中
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            for (int i = 0; i < twoSubjectList.size(); i++) {
                //获取每个二级分类
                EduSubject tSubject = twoSubjectList.get(i);
                //判断二级分类属于哪个一级分类 一级分类的id等于二级分类的parentid
                if (tSubject.getParentId().equals(oneSubject.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject, twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }

            }
            oneSubject.setChildren(twoFinalSubjectList);
            finalSubjectList.add(oneSubject);
        }
        return finalSubjectList;
    }
}
